﻿using Common;
using JQ50037.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace JQ50037.View
{

    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool isNotVisible)
            {
                return !isNotVisible;
            }
            throw new Exception("error param");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : Window
    {
        private DateTime firstDate = new DateTime(2024, 6, 25);

        public MainView()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }

        public MainView(string msg, string sn)
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
            tbDevSN.Text = $"设备序列号: {sn}";
            var licenseInfo = msg.Split(',').ToList();
            var strLicenseInfo = $"软件有效期至: {licenseInfo[0]}, 已使用: {(DateTime.Now - firstDate).Days}天";
            Mylog.Info(strLicenseInfo);

            Task.Run(() =>
            {
                while (true)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        tbSysTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    });
                    Thread.Sleep(1000);
                }
            });
        }

        private void Border_MouseLeftButtonDoubleDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (WindowState == WindowState.Normal)
                {
                    WindowState = WindowState.Maximized;
                }
                else
                {
                    WindowState = WindowState.Normal;
                }
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        private void btExit_Click(object sender, RoutedEventArgs e)
        {
            if (MyMessageBox.Show("提示", "是否退出窗口?", this) == true)
            {
                this.Close();
                Environment.Exit(0);
            }
        }
    }
}